<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Throughput extends CI_Controller {

	/**
	 * Array that contains the validation rules
	 * @access protected
	 * @var array
	 */
	protected $validation_rules = array(
		array(
			'field' => 'methods',
			'label' => 'methods',
			'rules' => 'trim|required'
		),
		array(
			'field' => 'throughput_start',
			'label' => 'starting at',
			'rules' => 'trim|required'
		),
		array(
			'field' => 'throughput_end',
			'label' => 'ending at',
			'rules' => 'trim|required'
		));
	
	/**
	 * Controller constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->model('throughput_m');
		
	}
	
	/**
	 * Default controller method, shows the default throughput page
	 * @access public
	 * @return void
	 */
	public function index()
	{
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules($this->validation_rules);
		
		$this->data->methods = $this->throughput_m->get_methods();
		
		if($this->data->methods)
		{
			$this->data->minMaxTime = prepareMinMaxTimes($this->throughput_m->get_min_max_time());
		}

		
		if($this->form_validation->run())
		{
			$this->data->xml = array(
					'throughput_start' => strtotime($this->input->post('throughput_start')),
					'throughput_end' => strtotime($this->input->post('throughput_end')),
					'method' => base64_encode($this->input->post('methods')));
			
			$startTime = strtotime($this->input->post('throughput_start'));
			$endTime = strtotime($this->input->post('throughput_end'));
			$completeRequests = $this->throughput_m->get_data($startTime, $endTime, $this->input->post('methods'), '1');
			$incompleteRequests = $this->throughput_m->get_data($startTime, $endTime, $this->input->post('methods'), '0');
			$method = $this->input->post('methods');
			
		} else {
			$method = "All methods";
			$startTime = $this->throughput_m->get_last_time();
			$endTime = $startTime + 84600;
			$completeRequests = $this->throughput_m->get_data($startTime, $endTime, '', '1');
			$incompleteRequests = $this->throughput_m->get_data($startTime, $endTime, '', '0');
		}
		
		$this->data->method = $method;
		$this->data->completeRequests = $completeRequests;
		$this->data->incompleteRequests = $incompleteRequests;
		$this->data->startTime = $startTime;
		$this->data->endTime = $endTime;
		$this->data->throughput = calculate_throughput($completeRequests, $startTime, $endTime);		

		$this->data->main_content = "pages/throughput";
		$this->load->view('template/template.php', $this->data);
	
	
	}
}

/* End of file throughput.php */
/* Location: ./application/controllers/throughput.php */